package com.xrun.risk.service.process.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xrun.cmap.WfMapUtils;
import com.xrun.common.constant.ThirdTypeConstants;
import com.xrun.ocenengine.dto.vo.AgentTokenAndTypeVo;
import com.xrun.risk.domain.WfRiskDeleteTask;
import com.xrun.risk.domain.WfRiskDeleteTaskAdter;
import com.xrun.risk.domain.WfRiskDeleteTaskAdterContentDetail;
import com.xrun.risk.dto.req.AdOrangeLoadingPageReq;
import com.xrun.risk.mapper.WfRiskDeleteTaskAdterContentDetailMapper;
import com.xrun.risk.service.process.Process;
import com.xrun.risk.util.OrangeLandingPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrangeSiteProcessImpl implements Process {
    @Autowired
    private OrangeLandingPageUtil orangeLandingPageUtil ;
    @Autowired
    private WfRiskDeleteTaskAdterContentDetailMapper wfRiskDeleteTaskAdterContentDetailMapper ;

    @Override
    public Object execute(WfRiskDeleteTask wfRiskDeleteTask,
                        WfRiskDeleteTaskAdter record,
                          AgentTokenAndTypeVo vo) {


        if(ThirdTypeConstants.AD.equals(vo.getThirdType())){
          return  this.dealForAD(wfRiskDeleteTask, record, vo);
        }else {
            //qc没有橙子落地页，ad才有
          return  null ;
        }


    }



    /**
     * ad 处理逻辑
     * @param wfRiskDeleteTask
     * @param record
     * @param vo
     * @return
     */
    private Object dealForAD(WfRiskDeleteTask wfRiskDeleteTask,
                        WfRiskDeleteTaskAdter record,
                          AgentTokenAndTypeVo vo) {


        AdOrangeLoadingPageReq req = new AdOrangeLoadingPageReq();
        req.setAdvertiserId(record.getAdvertiserId());
        req.setPage(1);
        req.setPageSize(1);

        //查询所有橙子落地页站点
        JSONObject orangeSite = orangeLandingPageUtil.getOrangeSite(WfMapUtils.get(vo.getTokenTableId()), req);
        JSONObject jsonObject = orangeSite.getJSONObject("data");
         Long total = jsonObject.getJSONObject("page_info").getLong("total_number");
        if (total>0){
            //含有站点，要提醒他删除失败
//            return "广告主有橙子落地页，请先删除";
            WfRiskDeleteTaskAdterContentDetail deleteTaskAdterContentDetail = new WfRiskDeleteTaskAdterContentDetail();
            deleteTaskAdterContentDetail.setId(IdWorker.getIdStr());
            deleteTaskAdterContentDetail.setAdvertiserId(record.getAdvertiserId());
            deleteTaskAdterContentDetail.setThirdType(vo.getThirdType());
            deleteTaskAdterContentDetail.setTaskId(wfRiskDeleteTask.getId());
            deleteTaskAdterContentDetail.setClearStatus("2");//0为待清理，1清理成功，2清理失败"
            deleteTaskAdterContentDetail.setFailReason("含有橙子落地页，无法删除");
            deleteTaskAdterContentDetail.setUserId(record.getUserId());
            deleteTaskAdterContentDetail.setDeptId(record.getDeptId());
            deleteTaskAdterContentDetail.setTargeType("OrangeSite");
            deleteTaskAdterContentDetail.setTargeId(null);
            deleteTaskAdterContentDetail.setCreateTime(new Date());
            wfRiskDeleteTaskAdterContentDetailMapper.insertWfRiskDeleteTaskAdterContentDetail(deleteTaskAdterContentDetail);
        }
        return null ;
    }
}
